package com.controller;

import com.alibaba.fastjson.JSON;
import com.entity.ShopGoods;
import com.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: RedisController
 * @Auther: zjd
 * @Date: 2018/8/29 16:15
 * @Description:
 */
@Controller
public class RedisController {
    @Autowired
    RedisUtil redisUtil;

    /**
     * @auther: zjd
     * @date: 16:23 2018/8/29
     * @param: []
     * @Description: 执行redis写/读/生命周期
     */
    @RequestMapping(value = "/getRedis")
    @ResponseBody
    public String getRedis() {
        redisUtil.set("name", "这是一条测试数据");
        Long resExpire = redisUtil.expire("name", 16000);//设置key过期时间
        if (resExpire > 0) {
            System.out.println("设置过期成功。");
        }
        String res = redisUtil.get("name");

        //测试Redis保存对象
        List<ShopGoods> saveList = new ArrayList<ShopGoods>();
        ShopGoods goods = new ShopGoods();
        goods.setNumber(1000);
        goods.setName("格力空调");
        saveList.add(goods);

        String str = JSON.toJSONString(saveList);
        redisUtil.set("goodsList", str);


        String strJSON = redisUtil.get("goodsList");
        List<ShopGoods> list = JSON.parseArray(strJSON, ShopGoods.class);
        System.out.println("Goods=" + list.get(0).getName());
        return "执行成功";
    }

}
